import User.User;
import User.Duser;

import javax.persistence.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@WebServlet(name = "self_login")
public class self_login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
        doSelf_login(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {

    }
    protected void doSelf_login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        User user = null;
        Duser duser = null;
        String email=request.getParameter("form-email");
        String passwd=request.getParameter("form-password");

        if(email.indexOf('@') != -1)
        {
            if((user = Treatment.isRight(email, passwd)) != null)
            {
                EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("MyDSTest");
                EntityManager entityManager = entityManagerFactory.createEntityManager();
                if(user.getDgutid() != null) {
                    duser = entityManager.find(Duser.class, user.getDgutid());
                }
                request.getSession().setAttribute("DGUT", duser);
                request.getSession().setAttribute("SELF", user);
                response.sendRedirect("/user.jsp");
            }
            else
            {
                System.out.println("邮箱或者密码错误！");
                response.sendRedirect("/error.jsp");
            }
        }
        else
        {
           if(Treatment.isRight_dgut(email))
            {
                EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("MyDSTest");
                EntityManager entityManager = entityManagerFactory.createEntityManager();
                duser = entityManager.find(Duser.class, email);
                Query query = entityManager.createNativeQuery("SELECT * FROM USER", User.class);
                List<User> list = query.getResultList();
                if (list != null) {
                    Iterator<User> iterator = list.iterator();
                    while(iterator.hasNext()) {
                        user = (User)iterator.next();
                        System.out.println(user);
                        if (user.getDgutid().equals(duser.getId())) {
                            request.getSession().setAttribute("SELF", user);
                            request.getSession().setAttribute("DGUT", duser);
                            response.sendRedirect("/user.jsp");
                        }
                     }
                }
            }
           else
            {
                System.out.println("不存在这个莞工用户！");
                response.sendRedirect("/error.jsp");
            }
        }
    }
}
